package com.troll.wanandroid.design.fragment

import android.content.Intent
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.troll.base.BaseFragment
import com.troll.eth.ex.cast
import com.troll.wanandroid.R
import com.troll.wanandroid.adapter.home.QuestionAdapter
import com.troll.wanandroid.design.activity.LinkActivity
import com.troll.wanandroid.model.ArticleIntroduction
import com.troll.wanandroid.model.QuestionList
import com.troll.wanandroid.viewmodel.QuestionViewModel

/**
 *
 * @Author:   TangPeng
 * @Time:     6/10/21
 * @Remark    问答相关
 */
class QuestionFragment : BaseFragment<QuestionViewModel>() {

    private var rv: RecyclerView? = null

    private var adapter: QuestionAdapter? = null

    override val layoutId: Int
        get() = R.layout.frag_question


    override fun flowOfData() {
        rv = findViewById(R.id.question_rv)
        adapter = QuestionAdapter()
        rv?.layoutManager =
            LinearLayoutManager(requireContext(), LinearLayoutManager.VERTICAL, false)
        rv?.adapter = adapter
    }

    override fun flowOfSetup() {
        with(viewModel) {
            data()
            liveQuestion.observe {
                adapter?.setNewData(it)
                adapter?.notifyDataSetChanged()
            }
        }


        adapter?.setOnItemClickListener { adapter, _, position ->
            adapter.getItem(position)?.cast<QuestionList> {
                val intent = Intent(requireContext(), LinkActivity::class.java)
                intent.putExtra("link", it.link)
                startActivity(intent)
            }
        }

    }

    override fun onRetryClick() {
        viewModel.data()
    }

}